package org.example.gui.listener;

import org.example.gui.panel.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * MainPanel 的监听器，监听 Toolbar 的按钮操作并切换面板
 * workingPanel有方法 .show(WorkingPanel p) 可以居中显示子面板 并 更新数据
 */

public class ToolBarListener implements ActionListener {
    private JButton prevButton = new JButton();

    @Override
    public void actionPerformed(ActionEvent e) {
        MainPanel panel = MainPanel.instance;
        JButton button = (JButton) e.getSource();

        button.setBackground(new Color(174, 242, 237));
        prevButton.setBackground(null);
        prevButton = button;

        if (button == panel.bBackup) {
            panel.workingPanel.show(BackupAndRecoverPanel.instance);
        }
        if (button == panel.bCategory) {
            panel.workingPanel.show(CategoryPanel.instance);
        }
        if (button == panel.bConfig) {
            panel.workingPanel.show(ConfigPanel.instance);
        }
        if (button == panel.bRecord) {
            panel.workingPanel.show(RecordPanel.instance);
        }
        if (button == panel.bReport) {
            panel.workingPanel.show(StatisticPanel.instance);
        }
        if (button == panel.bSpend) {
            panel.workingPanel.show(SpendPanel.instance);
        }
        if (button == panel.bHistory) {
            panel.workingPanel.show(HistoryPanel.instance);
        }
    }
}
